package com.graduate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduate.common.CustomException;
import com.graduate.entity.Category;
import com.graduate.entity.Goods;
import com.graduate.entity.SetMeal;
import com.graduate.mapper.CategoryMapper;
import com.graduate.service.CategoryService;
import com.graduate.service.GoodsService;
import com.graduate.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private SetMealService setMealService;
    //根据id删除分类，删除之前需要进行判断
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Goods> goodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id查询
        goodsLambdaQueryWrapper.eq(Goods::getCategoryId,id);
        int count1 = goodsService.count(goodsLambdaQueryWrapper);
        if (count1 > 0){
            //已经关联商品抛出一个业务异常
            throw new CustomException("当前分类下关联了商品，不能删除");
        }

        LambdaQueryWrapper<SetMeal> setMealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id查询
        setMealLambdaQueryWrapper.eq(SetMeal::getCategoryId,id);
        int count2 = setMealService.count(setMealLambdaQueryWrapper);
        if (count2 > 0){
            //已经关联套餐，抛出一个业务异常
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }

        //删除分类
        super.removeById(id);
    }
}
